package com.example.demo.controller;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.example.demo.bean.Result;
import com.example.demo.config.Constent;
import com.example.demo.config.Constent.SEQUENCE;
import com.example.demo.dao.ItemMapper;
import com.example.demo.dao.ItemTypeMapper;
import com.example.demo.dao.ShopItemMapper;
import com.example.demo.dao.SupplierMapper;
import com.example.demo.entity.Item;
import com.example.demo.entity.ItemType;
import com.example.demo.entity.ShopItem;
import com.example.demo.exception.SPIPException;
import com.example.demo.service.ItemService;
import com.example.demo.util.FileUtil;
import com.example.demo.util.TreeNodeUtil;
import com.example.demo.vo.ItemBean;
import com.example.demo.vo.TreeNode;
import com.google.common.collect.Lists;

/**
 * @author 88377761
 *
 */
@RestController
@RequestMapping("/item")
public class ItemController {

	@Autowired
	ItemTypeMapper itemTypeMapper;

	@Autowired
	ItemMapper itemMapper;

	@Autowired
	ItemService itemService;

	@Autowired
	ShopItemMapper shopItemMapper;

	@Autowired
	SupplierMapper supplierMapper;

	@RequestMapping("/listType")
	@ResponseBody
	public Result listByPage(ItemType type) {
		List<ItemType> types = itemTypeMapper.select(type);
		List<TreeNode> treeNodes = Lists.newArrayList();
		for (ItemType itemType : types) {
			TreeNode treeNode = new TreeNode(itemType.getId(), itemType.getpId(), itemType.getTypeName());
			treeNodes.add(treeNode);
		}
		return Result.getResultOk(TreeNodeUtil.listToTree(treeNodes));
	}

	@RequestMapping("/queryByCode")
	@ResponseBody
	public Result queryByCode(String code, Long shopId) throws SPIPException {
		if (StringUtils.isBlank(code)) {
			throw new SPIPException("40002", "未扫描到条码");
		}
		if (shopId == null) {
			throw new SPIPException("40002", "未设置当前门店");
		}
		Item parm = new Item();
		parm.setCode(code);
		Item item = itemMapper.queryOne(parm);
		if (item == null) {
			item = itemService.queryByApi(parm);
		}
		item.setImgUrl(Constent.getImgUrl(SEQUENCE.ITEM, item.getId()));
		ShopItem parm2 = new ShopItem();
		parm2.setShopId(shopId);
		parm2.setItemId(item.getId());
		ShopItem shopItem = shopItemMapper.selectOne(parm2);
		return Result.getResultOk(new ItemBean(item, shopItem));
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public Result edit(@RequestBody ItemBean itemBean) throws SPIPException {
		itemService.edit(itemBean);
		return Result.getResultOk(itemBean.getItem());
	}

	@RequestMapping(value = "/fileUp", method = RequestMethod.POST)
	@ResponseBody
	public Result fileUp(@ModelAttribute Item item, MultipartFile file) throws SPIPException {
		if (file != null && !file.isEmpty()) {
			FileUtil.upload(file, Constent.getFileUpUrl(SEQUENCE.ITEM, item.getId()));
		}
		return Result.getOk();
	}
}
